<?php


namespace app\model;

use think\Model;

class AdminUsers extends Model
{
    private $data;

    public $id;
    public $account;
    public $password;
    public $disableState;

    public function __construct(array $data = [])
    {
        parent::__construct($data);
        $this->data = array();
    }

    public function __destruct()
    {
        unset(
            $this->data
        );
    }

    public function read()
    {
        return $this->where('delete_state', 0)
            ->select()->toArray();
    }


    public function getCount()
    {
        return $this->where('delete_state', 0)->count();
    }

    public function signIn()
    {
        $data = $this->where('account', $this->account)
            ->where('password', $this->password)
            ->where('disable_state', 0)
            ->where('delete_state', 0)
            ->field('id, account, password')
            ->find();
        return boolval($data) == true ?
            $data->toArray() : false;
    }

    public function updateUserInfo()
    {
        $adminUserInfo = get_admin_user_info();
        $this->data['password'] = $this->password;
        return $this->where('account', $adminUserInfo['account'])
            ->save($this->data);
    }

    public function deleteAdminUser()
    {
        $this->data['delete_state'] = 1;
        $this->data['delete_time'] = time();
        return $this->where('id', $this->id)
            ->save($this->data);
    }

    public function updateDisableState()
    {
        $this->data['disable_state'] = $this->disableState;
        $this->data['disable_time'] = time();
        return $this->where('id', $this->id)
            ->save($this->data);
    }

    public function queryData()
    {
        return $this->where('id', $this->id)
            ->find()->toArray();
    }

    public function addAdminUser()
    {
        $this->data['account'] = $this->account;
        $this->data['password'] = $this->password;
        $this->data['create_time'] = time();
        $this->data['update_time'] = time();
        return $this->insertGetId($this->data);
    }

    public function editAdminUser()
    {
        $this->data['account'] = $this->account;
        $this->data['password'] = $this->password;
        $this->data['update_time'] = time();
        return $this->where('id', $this->id)
            ->save($this->data);
    }
}